package com.cyt.work.site.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.site.dao.TeacherDao;
import com.cyt.work.site.model.po.Teacher;
import com.cyt.work.site.model.vo.TeacherVo;
import com.cyt.work.site.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 微官网-教师风采Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-8 16:51:59
 */
@Service("teacherService")
public class TeacherServiceImpl extends BaseServiceImpl<Teacher> implements TeacherService {

    @Resource
    private TeacherDao teacherDao;
    @Resource
    private SysFileService sysFileService;

    @Autowired
    public void setBaseDao(TeacherDao teacherDao) {
        this.baseDao = teacherDao;
    }

    /**
     * 查询对象集合
     *
     * @param teacherVo 对象
     * @return List 对象集合
     * @author rong yuan
     * @date 2019-7-8 16:51:59
     */
    @Override
    public List<Teacher> listExample(TeacherVo teacherVo) {
        Example example = new Example(Teacher.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        if (StrUtil.isNotEmpty(teacherVo.getApplyId())) {
            criteria.andEqualTo("applyId", teacherVo.getApplyId());
        }
        if (StrUtil.isNotEmpty(teacherVo.getName())) {
            criteria.andLike("name", "%" + teacherVo.getName() + "%");
        }
        if (StrUtil.isNotEmpty(teacherVo.getSexType())) {
            criteria.andEqualTo("sexType", teacherVo.getSexType());
        }
        if (StrUtil.isNotEmpty(teacherVo.getNationType())) {
            criteria.andEqualTo("nationType", teacherVo.getNationType());
        }
        if (StrUtil.isNotEmpty(teacherVo.getDateBirth())) {
            criteria.andEqualTo("dateBirth", teacherVo.getDateBirth());
        }
        if (StrUtil.isNotEmpty(teacherVo.getPositionType())) {
            criteria.andEqualTo("positionType", teacherVo.getPositionType());
        }
        return teacherDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param teacherVo 接受参数对象
     * @param pageNum   分页数
     * @param pageSize  每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 16:51:59
     */
    @Override
    public ReturnResult page(TeacherVo teacherVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<Teacher> list = listExample(teacherVo);
        PageInfo<Teacher> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param teacherVo 参数接受对象
     * @param sysUser   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 16:51:59
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(TeacherVo teacherVo, SysUser sysUser) {
        if (StrUtil.isEmpty(teacherVo.getId())) {
            Teacher teacher = new Teacher(sysUser);
            teacher.setApplyId(teacherVo.getApplyId());
            teacher.setName(teacherVo.getName());
            teacher.setPhoto(teacherVo.getPhoto());
            teacher.setPath(getFilePath(teacherVo.getPhoto()));
            teacher.setSexType(teacherVo.getSexType());
            teacher.setNationType(teacherVo.getNationType());
            teacher.setDateBirth(teacherVo.getDateBirth());
            teacher.setBirthPlace(teacherVo.getBirthPlace());
            teacher.setPositionType(teacherVo.getPositionType());
            teacher.setDescription(teacherVo.getDescription());
            int num = teacherDao.insert(teacher);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            Teacher teacher = teacherDao.selectByPrimaryKey(teacherVo.getId());
            teacher.setApplyId(teacherVo.getApplyId());
            teacher.setName(teacherVo.getName());
            teacher.setPhoto(teacherVo.getPhoto());
            teacher.setPath(getFilePath(teacherVo.getPhoto()));
            teacher.setSexType(teacherVo.getSexType());
            teacher.setNationType(teacherVo.getNationType());
            teacher.setDateBirth(teacherVo.getDateBirth());
            teacher.setBirthPlace(teacherVo.getBirthPlace());
            teacher.setPositionType(teacherVo.getPositionType());
            teacher.setDescription(teacherVo.getDescription());
            teacher.setUpdateTime(new Date());
            teacher.setUpdateUser(sysUser.getId());
            int num = teacherDao.updateByPrimaryKey(teacher);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }

    /**
     * 根据条件逻辑删除
     *
     * @param teacherVo 参数接受对象
     * @param sysUser   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 16:51:59
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(TeacherVo teacherVo, SysUser sysUser) {
        //要修改的字段
        Teacher teacher = new Teacher();
        teacher.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        teacher.setUpdateUser(sysUser.getId());
        teacher.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(Teacher.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(teacherVo.getId())) {
            criteria.andIn("id", Arrays.asList(teacherVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        teacherDao.updateByExampleSelective(teacher, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param teacherVo 参数接受对象
     * @param sysUser   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 16:51:59
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(TeacherVo teacherVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(Teacher.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(teacherVo.getId())) {
            criteria.andIn("id", Arrays.asList(teacherVo.getId().split(",")));
        }
        teacherDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * list查询
     *
     * @param teacherVo 参数接受对象
     * @return List<TeacherVo>
     * @author rong yuan
     * @date 2019/7/10 08:32
     */
    @Override
    public List<TeacherVo> list(TeacherVo teacherVo) {
        return teacherDao.selectByCondition(teacherVo);
    }

    /**
     * 分页查询
     *
     * @param teacherVo 接受参数对象
     * @param pageNum   分页数
     * @param pageSize  每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 16:51:59
     */
    @Override
    public ReturnResult pageVo(TeacherVo teacherVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TeacherVo> list = teacherDao.selectByCondition(teacherVo);
        PageInfo<TeacherVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }
}